package com.dys.testarray;

/**
 * @className: com.dys.testarray.TwoArrayTest
 * @description: 二维数组
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/07/13 下午10:27
 */
public class TwoArrayTest {
    public static void main(String[] args) {
        initTowArray();
    }

    public static void testTwoArray() {
        // 1.定义一个二维数组
        int[][] arr = new int[3][];
        int[] a1 = {1, 2, 3};
        int[] a2 = {4, 5, 6};
        int[] a3 = {7, 8, 9};
        arr[0] = a1;
        arr[1] = a2;
        arr[2] = a3;
        // 2.读取6这个元素
        System.out.println(arr[1][2]);
        // 3.对二维数组遍历
        // 外层普通for+内层普通for
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void testTwoArray2() {
        // 1.定义一个二维数组
        int[][] arr = new int[3][];
        int[] a1 = {1, 2, 3};
        int[] a2 = {4, 5, 6};
        int[] a3 = {7, 8, 9};
        arr[0] = a1;
        arr[1] = a2;
        arr[2] = a3;
        // 2.读取6这个元素
        System.out.println(arr[1][2]);
        // 3.对二维数组遍历
        // 外层普通for+内层增强for
        for (int i = 0; i < arr.length; i++) {
            for (int num : arr[i]) {
                System.out.print(num + " ");
            }
            System.out.println();
        }
    }

    public static void testTwoArray3() {
        // 1.定义一个二维数组
        int[][] arr = new int[3][];
        int[] a1 = {1, 2, 3};
        int[] a2 = {4, 5, 6};
        int[] a3 = {7, 8, 9};
        arr[0] = a1;
        arr[1] = a2;
        arr[2] = a3;
        // 2.读取6这个元素
        System.out.println(arr[1][2]);
        // 3.对二维数组遍历
        // 外层增强for+内层增强for
        for (int[] num : arr) {
            for (int num2 : num) {
                System.out.print(num2 + " ");
            }
            System.out.println();
        }
    }

    public static void testTwoArray4() {
        // 1.定义一个二维数组
        int[][] arr = new int[3][];
        int[] a1 = {1, 2, 3};
        int[] a2 = {4, 5, 6};
        int[] a3 = {7, 8, 9};
        arr[0] = a1;
        arr[1] = a2;
        arr[2] = a3;
        // 2.读取6这个元素
        System.out.println(arr[1][2]);
        // 3.对二维数组遍历
        // 外层增强for+内层普通for
        for (int[] num : arr) {
            for (int i = 0; i < num.length; i++) {
                System.out.print(num[i] + " ");
            }
            System.out.println();
        }
    }

    /**
     * 二维数组初始化方式
     */
    public static void initTowArray() {
        // 1.静态初始化
        int[][] arr = {{1, 2, 3}, {4, 5, 6}};
        int[][] arr2 = new int[][]{{1, 2, 3}, {4, 5, 6}};

        // 2.动态初始化
        int[][] arr3 = new int[3][2];
        arr3[1] = new int[]{1, 2, 3, 4};
        for (int[] a : arr3) {
            for (int n : a) {
                System.out.print(n + "\t");
            }
            System.out.println();
        }

        // 3.默认初始化
        int[][] arr4 = new int[3][2];
        System.out.println("默认初始化");
        for (int[] a : arr4) {
            for (int n : a) {
                System.out.print(n + "\t");
            }
            System.out.println();
        }
    }
}
